/*
 * @Descripttion: 系统消息处理
 * @version: 1.0
 * @LastEditors: lzt
 * @LastEditTime: 2023-12-06
 */

import {ElNotification} from "element-plus";
import i18n from "@/locales";

const formRule = {
    addRequired: function (formRules, prop, message,isNumber) {
        let rules = formRules[prop];
        if (!rules) {
            rules = [];
        }
        let rule = {
            id: 'required',
            required: true,
            message: message,
            trigger: "change",
            type:isNumber||''
        };
        rules.push(rule);
        formRules[prop] = rules;
    },
    removeRequired: function (formRules, prop) {
        let rules = formRules[prop];
        if (!rules) {
            rules = [];
        }
        rules = rules.filter(rule => rule.id !== 'required');
        formRules[prop] = rules;
    },
    addLength: function (formRules, prop, label, minLength, maxLength) {
        let rules = formRules[prop];
        if (!rules) {
            rules = [];
        }
        rules = rules.filter(rule => rule.id !== 'length');
        let rule;
        if (minLength && maxLength) {
            rule = {
                id: 'length',
                min: minLength,
                max: maxLength,
                message: label + '的长度必须在 ' + minLength + ' 到 ' + maxLength + ' 个字符',
                trigger: 'change'
            };
        } else if (minLength) {
            rule = {
                id: 'length',
                min: minLength,
                message: label + '不能小于' + minLength + '个字符',
                trigger: 'change'
            };
        } else if (maxLength) {
            rule = {
                id: 'length',
                max: maxLength,
                message: label + '不能大于' + maxLength + '个字符',
                trigger: 'change'
            };
        }
        if(rule){
            rules.push(rule);
        }
        formRules[prop] = rules;
    },
    removeLength: function (formRules, prop) {
        let rules = formRules[prop];
        if (!rules) {
            rules = [];
        }
        rules = rules.filter(rule => rule.id !== 'length');
        formRules[prop] = rules;
    },
	updateColumnFormatter(column){
		let property = column.code;
		if(column.dynamicCode){
			property = column.dynamicCode+'.'+ property;
		}
		if(column.nodeName){
			property = column.nodeName+"."+property;
		}
		column.property = property;

		if(column.widgetType==='select'||column.widgetType==='tree-select'||column.widgetType==='radio'||column.widgetType==='cascade'||column.widgetType==='selector'){
			column.formatter="DICT";
			column.key=column.dataSourceCode;
		}
		if(column.widgetType==='date'||column.widgetType==='datetime'){
			column.formatter=column.widgetType.toUpperCase();
		}
	},
}


export default formRule
